﻿#include "FileNameModel.h"

QFileNameModel::QFileNameModel(QObject *parent)
	: QFileSystemModel(parent)
{
	m_filters = QDir::AllEntries | QDir::NoDot | QDir::NoSymLinks;
	setReadOnly(true);
	setFilter(m_filters);
	setNameFilterDisables(false);

	sort(0);
}

QFileNameModel::~QFileNameModel()
{

}

int QFileNameModel::columnCount(const QModelIndex &parent) const
{
	return 1;
}

QVariant QFileNameModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
	if (role != Qt::DisplayRole)
	{
		return QVariant();
	}

	if (orientation == Qt::Horizontal)
	{
		switch (section)
		{
		case 0:
			return "File Name";
		}
	}

	return QFileSystemModel::headerData(section, orientation, role);
}

void QFileNameModel::updateFilter(const QStringList& qslSelectedFiles)
{
	QString qsRootPath = rootPath();

	QStringList qslNames;
	QStringList::const_iterator constIterator;
	for (constIterator = qslSelectedFiles.constBegin(); constIterator != qslSelectedFiles.constEnd(); ++constIterator)
	{
		QString qsFile = *constIterator;
		QFileInfo info(qsFile);
		QString qsPath = info.absolutePath();
		if (qsPath != qsRootPath)
		{
			continue;
		}
		QString qsName = info.fileName();
		qslNames.append(qsName);
	}

	QStringList qslFilter;
	QDir dir(qsRootPath);
	dir.setFilter(m_filters);
	QFileInfoList list = dir.entryInfoList();
	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo = list.at(i);
		QString qsName = fileInfo.fileName();
		if (fileInfo.isDir() || !qslNames.contains(qsName))
		{
			qslFilter.append(qsName);
		}
	}

	setNameFilters(qslFilter);
}
